<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>友情链接</title>
		<link rel="stylesheet" href="{{ assert(path='admin/component/pear/css/pear.css') | safe }}" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">名称</label>
							<div class="layui-input-inline">
								<input type="text" name="title" placeholder="" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">链接</label>
							<div class="layui-input-inline">
								<input type="text" name="url" placeholder="" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">跳转方式</label>
							<div class="layui-input-inline">
								<select name="target" lay-verify="">
									<option value="">全部</option>
									<option value="_blank">跳出页面</option>
									<option value="_self">当前页面</option>
								</select>
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">状态</label>
							<div class="layui-input-inline">
								<select name="status" lay-verify="">
									<option value="-1">全部</option>
									<option value="1">启用</option>
									<option value="0">禁用</option>
								</select>
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="friendlink-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>

		<div class="layui-card">
			<div class="layui-card-body">
				<table id="friendlink-table" lay-filter="friendlink-table"></table>
			</div>
		</div>

		{% raw %}
		<script type="text/html" id="friendlink-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		</script>

		<script type="text/html" id="friendlink-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="friendlink-name">
			<span lay-event="detail">
				{{ d.title }}
			</span>
		</script>

		<script type="text/html" id="friendlink-target">
			{{# if (d.target == "_blank") { }}
				<span class="layui-badge layui-bg-green">跳出页面</span>
			{{# } else if (d.target == "_self") { }}
				<span class="layui-badge layui-bg-blue">当前页面</span>
		    {{# } else { }}
		    	{{ d.target }}
		    {{# } }}
		</script>		

		<script type="text/html" id="friendlink-enable">
			<input type="checkbox" name="enable" value="{{d.id}}"
				lay-skin="switch" 
				lay-text="启用|禁用" 
				lay-filter="friendlink-enable" 
				{{ d.status == 1 ? 'checked' : '' }}>
		</script>

		<script type="text/html" id="friendlink-createTime">
			{{# if (d.add_time > 0) { }}
				{{layui.util.toDateString(d.add_time * 1000, 'yyyy-MM-dd HH:mm:ss')}}
		    {{# } else { }}
		    	--
		    {{# } }}
		</script>
		{% endraw %}

		<script src="{{ assert(path='admin/component/layui/layui.js') | safe }}"></script>
		<script src="{{ assert(path='admin/component/pear/pear.js') | safe }}"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let cols = [
					[
						{
							title: '名称',
							field: 'title',
							align: 'left',
							templet: '#friendlink-name'
						},
						{
							title: '链接',
							field: 'url',
							align: 'left',
						},
						{
							title: '跳转方式',
							field: 'target',
							align: 'left',
							width: 100,
							templet: '#friendlink-target'
						},
						{
							title: '排序',
							field: 'sort',
							align: 'center',
							width: 80,
						},
						{
							title: '添加时间',
							field: 'createTime',
							align: 'left',
							templet: '#friendlink-createTime',
							width: 160,
						},
						{
							title: '启用',
							field: 'enable',
							align: 'center',
							templet: '#friendlink-enable',
							width: 150
						},
						{
							title: '操作',
							toolbar: '#friendlink-bar',
							align: 'left',
							width: 150
						}
					]
				]

				table.render({
					elem: '#friendlink-table',
					url: "{{ url_for(name='admin.friendlink-list') | safe }}",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#friendlink-toolbar',
					parseData: function(res) {
						return {
							"code": res.code,
							"count": res.data.count,
							"data": res.data.list,
						};
					},
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(friendlink-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					} else if (obj.event === 'detail') {
						window.detail(obj);
					}
				});

				form.on('submit(friendlink-query)', function(data) {
					table.reload('friendlink-table', {
						where: data.field
					})
					return false;
				});
				
				table.on('toolbar(friendlink-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					}
				});

				form.on('switch(friendlink-enable)', function(obj) {
					var status = 0;
					if (obj.elem.checked == true) {
						status = 1;
					}

					$.ajax({
						url: "{{ url_for(name='admin.friendlink-status') | safe }}?id=" + this.value,
						data: {
							status: status,
						},
						type: 'post',
						dataType: 'json',
						success: function(result) {
							if (result.code == 0) {
								layer.tips(result.message, obj.othis);
							} else {
								layer.msg(result.message, {
									icon: 2,
									time: 1000
								});
							}
						}
					});
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增',
						shade: 0.1,
						area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'310px'],
						content: "{{ url_for(name='admin.friendlink-create') | safe }}"
					});
				}

				window.edit = function(obj) {
					layer.open({
						type: 2,
						title: '修改',
						shade: 0.1,
						area: ['500px', '400px'],
						content: "{{ url_for(name='admin.friendlink-update') | safe }}?id=" + obj.data['id'],
					});
				}

				window.detail = function(obj) {
					layer.open({
						type: 2,
						title: '链接详情',
						shade: 0.1,
						area: ['500px', '400px'],
						content: "{{ url_for(name='admin.friendlink-detail') | safe }}?id=" + obj.data['id'],
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该链接', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();

						$.ajax({
							url: "{{ url_for(name='admin.friendlink-delete') | safe }}",
							data: {
								id: obj.data['id'],
							},
							type: 'post',
							dataType: 'json',
							success: function(result) {
								layer.close(loading);

								if (result.code == 0) {
									layer.msg(result.message, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.message, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('friendlink-table');
				}
			})
		</script>
	</body>
</html>
